package com.leyou.item.web;

import com.baomidou.mybatisplus.extension.api.R;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("brand")
public class BrandController {

    /*private final BrandService brandService;

    public BrandController(BrandService brandService) {
        this.brandService = brandService;
    }*/
    @Autowired
    private BrandService brandService;


    /**
     * todo 分页查询
     * @param key
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO<BrandDTO>> quaryPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows
    ) {

        return ResponseEntity.ok(brandService.quaryPage(key,page,rows));
    }

    /**
     * todo 根据品牌ids 查询对应的品牌集合
     * @param ids
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<BrandDTO>> listBrandByBid(@RequestParam("ids")List<Long> ids){

        return ResponseEntity.ok(brandService.listBrandByBid(ids));

    }



    /**
     * todo 根据分类id 查询品牌
     * @param id
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCid(@RequestParam("id")Long id){

        return ResponseEntity.ok(brandService.queryBrandByCid(id));
    }

    /**
     * todo 根据id 查询品牌对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id")Long id){

        return ResponseEntity.ok(brandService.queryBrandById(id));
    }


    /**
     * todo 添加品牌对象
     * @param brandDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO){

        brandService.addBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * todo 更新品牌
     * @param brandDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
        brandService.updateBrand(brandDTO);
        return ResponseEntity.status(204).build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("id")Long id){
        brandService.deleteBrand(id);
        return ResponseEntity.status(200).build();

    }



}